package com.shinhye.demoproject;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.Random;

public class AlipayActivity extends AppCompatActivity {

    private RelativeLayout redmoney_panel;
    private ImageView[] ivs = new ImageView[3];
    private int[] colors = {Color.RED, Color.BLUE, Color.GREEN};
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    setupRedMan();
                    this.sendEmptyMessageDelayed(0, 400);
                    break;
                case 1:
                    redmoney_panel.setVisibility(View.GONE);
                    this.removeCallbacksAndMessages(null);
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alipay);

        redmoney_panel = (RelativeLayout) findViewById(R.id.redmoney_panel);
    }

    private void startRedMoney2Show() {
        handler.sendEmptyMessageDelayed(0, 400);
        handler.sendEmptyMessageDelayed(1, 4000);
    }

    private void setupRedMan() {
        for(int i = 0; i < 3; i++) {
            ImageView iv = ivs[i];
            if(iv == null) {
                iv = new ImageView(AlipayActivity.this);
                ivs[i] = iv;
                redmoney_panel.addView(iv, i, new ViewGroup.LayoutParams(100, 100));
            }
            Random random = new Random();
            int index = random.nextInt(colors.length);
            iv.setBackgroundColor(colors[index]);

            int max_height = redmoney_panel.getHeight();
            int max_width = redmoney_panel.getWidth();

            int t = random.nextInt(max_height - 300);
            int l = random.nextInt(max_width - 300);

            ViewGroup.MarginLayoutParams lps = (ViewGroup.MarginLayoutParams) iv.getLayoutParams();
            lps.topMargin = t;
            lps.leftMargin = l;
            iv.setLayoutParams(lps);
        }
    }

    public void onClickButton(View v) {
        switch (v.getId()) {
            case R.id.button1:
                redmoney_panel.setVisibility(View.VISIBLE);
                startRedMoney2Show();
                break;
        }
    }
}
